/**
  ******************************************************************************
  * @file    gt32f030_lvd.c
  * @author  GT Application Team
  * @version V1.0.0
  * @date    03-January-2025      
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 Giantec Semicondutor Inc</center></h2>
  *
  *             http://www.giantec-semi.com/
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "gt32f030_rcc.h"
#include "gt32f030_lvd.h"

 /** @addtogroup GT32F030_StdPeriph_Driver
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/



/** @defgroup LVD 
  * @brief LVD driver modules
  * @{
  */ 

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup LVD_Private_Functions
  * @{
  */ 

/** @defgroup LVD_Init Initialization functions
 *  @brief   Initialization and functions
 *
@verbatim    
 ===============================================================================
              ##### Initialization functions #####
 ===============================================================================
@endverbatim
  * @{
  */
/**
  * @brief  Deinitializes the BEEP peripheral registers to their default reset values.
  * @param  None
  * @retval None
  */
void LVD_DeInit(void)
{
	RCC_PeriphResetCmd(RCC_PeriphReset_VCLVD, ENABLE);
	RCC_PeriphResetCmd(RCC_PeriphReset_VCLVD, DISABLE);
}

/**
  * @brief  Initializes the BEEP peripheral according to the specified
  *         parameters in the BEEP_InitStruct .
  * @param  BEEP_InitStruct: pointer to a BEEP_InitTypeDef structure that contains
  *         the configuration information.
  * @retval None
  */
void LVD_Init(LVD_InitTypeDef* LVD_InitStruct)
{
	/* Check the parameters */
	assert_param(IS_LVD_VSRC(LVD_InitStruct->LVD_VSourceSel));
  assert_param(IS_LVD_VOLT(LVD_InitStruct->LVD_VoltSel));
  assert_param(IS_LVD_ACTMODE(LVD_InitStruct->LVD_ActiveMode)); 
	assert_param(IS_FUNCTIONAL_STATE(LVD_InitStruct->LVD_Cmd));
 
  /*---------------------------- LVD CR Configuration -----------------*/
  /*
	LVD->CR   &=~0x60;
	LVD->VSEL &=~0x7F;
	
  LVD->VSEL   |= (LVD_InitStruct->LVD_VSourceSel<<4)   | \
	               (LVD_InitStruct->LVD_VoltSel);
	
	LVD->CR     |= (LVD_InitStruct->LVD_ActiveMode<<6)   | \
	               (LVD_InitStruct->LVD_Cmd<<5);
  */
  VCLVD->LVDCR1 &= ~((uint32_t)(0x00170060));
  VCLVD->LVDCR1 |= (uint32_t)((LVD_InitStruct->LVD_VSourceSel << 20) |
                              (LVD_InitStruct->LVD_VoltSel << 16) |
                              (LVD_InitStruct->LVD_ActiveMode << 6) |
                              (LVD_InitStruct->LVD_Cmd << 5));
}

/**
  * @brief  Fills each BEEP_InitStruct member with its default value.
  * @param  BEEP_InitStruct: pointer to a BEEP_InitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void LVD_StructInit(LVD_InitTypeDef* LVD_InitStruct)
{
	LVD_InitStruct->LVD_VSourceSel = LVDVSourceSel_Vdd;
	LVD_InitStruct->LVD_VoltSel    = LVDVoltSel_1_8V;
	LVD_InitStruct->LVD_ActiveMode = LVD_ActiveMode_Interrupt;
	LVD_InitStruct->LVD_Cmd        = DISABLE;
}
/**
  * @}  
  */ 

/** @defgroup LVD Operation and Configuration functions
 *  @brief   Operation and Configuration functions
 *
@verbatim   
 ===============================================================================
           ##### Operation and Configuration functions #####
 ===============================================================================  
@endverbatim
  * @{
  */
/**
  * @brief  Enables or disables the LVD .
  * @param  NewState: new state of the LVD.
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void LVD_Cmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    VCLVD->LVDCR1 |= LVD_CR_ENABLE;
  }
  else
  {
    VCLVD->LVDCR1 &= ~LVD_CR_ENABLE;
  }	
}
/**
  * @}  
  */ 

/** @defgroup LVD_Group2 LVD Interrupt functions configuration function
 *  @brief   LVD Interrupt functions configuration function
 *
@verbatim   
 ===============================================================================
           ##### LVD Interrupt functions configuration function #####
 ===============================================================================  
@endverbatim
  * @{
  */

/**
  * @brief  Initializes the LVD Interrupt according to the specified parameters in the LVDInt_InitStruct.  
  * @param  LVDInt_InitStruct: pointer to a LVDInt_InitTypeDef structure that contains
  *         the configuration information for the specified LVD Interrupt.
  * @retval None
  */
void LVDInt_Init(LVDInt_InitTypeDef* LVDInt_InitStruct)
{
  /* Check the parameters */
  assert_param(IS_LVD_INTTRG(LVDInt_InitStruct->LVDInt_Trigger));
  assert_param(IS_FUNCTIONAL_STATE(LVDInt_InitStruct->LVDInt_Cmd));
	
  /*
  LVD->CR &= ~0xF000;
	if (LVDInt_InitStruct->LVDInt_Cmd != DISABLE)
  {
    LVD->CR |= LVDInt_InitStruct->LVDInt_Trigger|LVD_CR_INTENABLE;
  }
  else
  {
    LVD->CR |= LVDInt_InitStruct->LVDInt_Trigger;
  }
  */
  VCLVD->LVDCR1 &= ~((uint32_t)(0x07008000));
  if (LVDInt_InitStruct->LVDInt_Cmd != DISABLE) {
    VCLVD->LVDCR1 |= (LVDInt_InitStruct->LVDInt_Trigger | LVD_CR_INTENABLE);
  } else {
    VCLVD->LVDCR1 |= LVDInt_InitStruct->LVDInt_Trigger;
  }
}

/**
  * @brief  
  * @param  LVDInt_InitStruct : pointer to a LVDInt_InitTypeDef structure which will be initialized.
  * @retval None
  */
void LVDInt_StructInit(LVDInt_InitTypeDef* LVDInt_InitStruct)
{
  /* Reset GPIO init structure parameters values */
  LVDInt_InitStruct->LVDInt_Trigger = 0;
  LVDInt_InitStruct->LVDInt_Cmd     = DISABLE;
}


/** @defgroup LVD_Group3 LVD Filter functions configuration function
 *  @brief   LVD Filter functions configuration function
 *
@verbatim   
 ===============================================================================
           ##### LVD Filter functions configuration function #####
 ===============================================================================  
@endverbatim
  * @{
  */

/**
  * @brief  Initializes the LVD Filter according to the specified parameters in the LVDFilter_InitStruct.  
  * @param  LVDFilter_InitStruct: pointer to a LVDFilter_InitTypeDef structure that contains
  *         the configuration information for the specified LVD Filter.
  * @retval None
  */
void LVDFilter_Init(LVDFilter_InitTypeDef* LVDFilter_InitStruct)
{
  /* Check the parameters */
  assert_param(IS_LVD_FLTNUM(LVDFilter_InitStruct->LVDFilter_Number));
	assert_param(IS_LVD_FLTSRC(LVDFilter_InitStruct->LVDFilter_Source));
  assert_param(IS_FUNCTIONAL_STATE(LVDFilter_InitStruct->LVDFilter_Cmd));
	
  /*
  LVD->CR &= ~0xFFFF0380; //0000 0011 1000 0000
	if (LVDFilter_InitStruct->LVDFilter_Cmd != DISABLE)
  {
    LVD->CR |= (LVDFilter_InitStruct->LVDFilter_Number<<16)  | \
		           (LVDFilter_InitStruct->LVDFilter_Source<<8)   |
		            LVD_CR_FLTENABLE;
  }
  else
  {
    LVD->CR |= (LVDFilter_InitStruct->LVDFilter_Number<<16)  | \
		           (LVDFilter_InitStruct->LVDFilter_Source<<8);
  }	
  */

  VCLVD->LVDCR2 &= ~((uint32_t)(0xFFFF0001));
  VCLVD->LVDCR1 &= ~((uint32_t)(0x00000100));
  if (LVDFilter_InitStruct->LVDFilter_Cmd != DISABLE) {
    VCLVD->LVDCR2 |= ((LVDFilter_InitStruct->LVDFilter_Number << 16) | LVD_CR_FLTENABLE);
    VCLVD->LVDCR1 |= (LVDFilter_InitStruct->LVDFilter_Source << 8);
  } else {
    VCLVD->LVDCR2 |= (LVDFilter_InitStruct->LVDFilter_Number << 16);
    VCLVD->LVDCR1 |= (LVDFilter_InitStruct->LVDFilter_Source << 8);
  }
}

/**
  * @brief  
  * @param  LVDInt_InitStruct : pointer to a LVDInt_InitTypeDef structure which will be initialized.
  * @retval None
  */
void LVDFilter_StructInit(LVDFilter_InitTypeDef* LVDFilter_InitStruct)
{
  /* Reset GPIO init structure parameters values */
  LVDFilter_InitStruct->LVDFilter_Number = 0;
  /*
  LVDFilter_InitStruct->LVDFilter_Source = LVD_FilterSource_NULL;
  */
  LVDFilter_InitStruct->LVDFilter_Source = LVD_FilterSource_PCLK;
	LVDFilter_InitStruct->LVDFilter_Cmd    = DISABLE;
}

/**
  * @}
  */

/**
  * @}
  */
/*
ITStatus LVD_GetIT(void)
{
  FlagStatus bitstatus = RESET;

  if (LVD->IR&0x01)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return bitstatus;  
}
*/

/**
  * @}
  */
void LVD_ClearIT(void)
{
  /*
  LVD->IR = 0;
  */

  VCLVD->ISR = 0x19d00004;
}

/**
  * @}  
  */

/**
  * @}  
  */ 

/**
  * @}
  */ 
/************************ (C) COPYRIGHT Giantec Semicondutor Inc *****END OF FILE****/
